local muyan = fk.CreateSkill {
  name = "emo__muyan",
}

muyan:addEffect("active", {
  anim_type = "support",
  prompt = "#emo__muyan",
  card_num = 0,
  target_num = 0,
  times = function (self, player)
    return 1 - player:usedSkillTimes(muyan.name, Player.HistoryPhase)
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(muyan.name, Player.HistoryPhase) < 1
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local num = 4
    if player:getMark("emo__muyan_extraturn") ~= 0 then
      room:setPlayerMark(player, "emo__muyan_extraturn", 0)
      num = num + 2
    end
    local ids = room:getNCards(num)
    room:moveCardTo(ids, Card.Processing, nil, fk.ReasonPut, muyan.name, nil, true, player)
    room:delay(500)

    local to_get = room:askToPoxi(player, {
      poxi_type = "emo__muyan_poxi", cancelable = false,
      data = {{ self.name, ids }},
    })
    local choices = {"emo__muyan_maxcard", "emo__muyan_extraturn"}
    local choice = choices[2]
    if #to_get == 1 then
      choice = room:askToChoice(player, {skill_name = muyan.name, choices = choices, prompt = "#emo__muyan-choice"})
    elseif Fk:getCardById(to_get[1]).type == Fk:getCardById(to_get[2]).type then
      choice = choices[1]
    end
    room:setPlayerMark(player, choice, 1)

    room:moveCardTo(to_get, Player.Hand, player, fk.ReasonPrey, muyan.name, nil, true, player,
    choice == "emo__muyan_maxcard" and "@@emo__muyan-inhand-turn" or nil)
    room:cleanProcessingArea(ids, muyan.name)
  end,
})

Fk:addPoxiMethod{
  name = "emo__muyan_poxi",
  card_filter = function(to_select, selected)
    if #selected < 2 then return true end
    return (Fk:getCardById(selected[1]).type == Fk:getCardById(selected[2]).type) ==
    (table.find(selected, function(id) return Fk:getCardById(id).type == Fk:getCardById(to_select).type end) ~= nil)
  end,
  feasible = function(selected)
    return #selected > 0
  end,
  default_choice = function(data)
    local cards = data[1][2]
    return table.filter(cards, function(id) return Fk:getCardById(id).type == Fk:getCardById(cards[1]).type end)
  end,
  prompt = function ()
    return Fk:translate("#emo__muyan-get")
  end,
}

muyan:addEffect("maxcards", {
  exclude_from = function (self, player, card)
    return card:getMark("@@emo__muyan-inhand-turn") ~= 0
  end,
})

Fk:loadTranslationTable{
  ["emo__muyan"] = "沐焰",
  [":emo__muyan"] = "出牌阶段限一次，你可以亮出牌堆顶四张牌并选一项：<br>"..
  "①获得其中一种类型的牌，本回合这些牌不计入手牌上限；<br>"..
  "②获得其中不同类型的牌，下次额外亮出两张牌。",

  ["@@emo__muyan-inhand-turn"] = "沐焰",
  ["emo__muyan_poxi"] = "沐焰",
  ["#emo__muyan"] = "沐焰:亮出牌堆顶四张牌，获得其中类型相同或不同的牌",
  ["#emo__muyan-get"] = "沐焰：选择要获得的牌",
  ["emo__muyan_maxcard"] = "不计入手牌上限",
  ["emo__muyan_extraturn"] = "下次额外亮2张",
}

return muyan
